package cn.uai.activiti8.approval.service.impl;

import cn.uai.activiti8.approval.service.DeployService;
import cn.uai.activiti8.dto.Result;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.spring.boot.ActivitiProperties;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @desc
 * @author uai
 * @since 2025/8/7 11:29
 * @version 1.0
 */
@Service
public class DeployServiceImpl implements DeployService {

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private ActivitiProperties activitiProperties;

    @Override
    public String deploy(String body) {
        JSONObject jsonObject = JSONObject.parseObject(body);
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource(activitiProperties.getProcessDefinitionLocationPrefix() + jsonObject.getString("processXmlFileName"))
                .name(jsonObject.getString("processName"))
                .deploy();
        Map<String, Object> map = new HashMap<>();
        map.put("deployId", deploy.getId());
        map.put("deployName", deploy.getName());
        map.put("deployTime", deploy.getDeploymentTime());
        return Result.success(map);
    }

    @Override
    public String processDefinitionQuery(String deployId) {
        List<Map<String, String>> processDefinitionsList = new ArrayList<>();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployId).list();
        Optional<List<ProcessDefinition>> listOptional = Optional.ofNullable(list);
        if (listOptional.isPresent()) {
            List<ProcessDefinition> processDefinitions = listOptional.get();
            processDefinitions.forEach(processDefinition -> {
                Map<String, String> map = new HashMap<>();
                map.put("processDefinitionId", processDefinition.getId());
                map.put("processDefinitionKey", processDefinition.getKey());
                map.put("processDefinitionName", processDefinition.getName());
                processDefinitionsList.add(map);
            });
        }
        return Result.success(processDefinitionsList);
    }

    @Override
    public String processDefinitionDeployQuery(String processDefinitionName) {
        List<Map<String, String>> processDefinitionsList = new ArrayList<>();
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionName(processDefinitionName).list();
        if (null == list || list.isEmpty()) {
            return Result.success(processDefinitionsList);
        }
        list.forEach(processDefinition -> {
            Map<String, String> map = new HashMap<>();
            map.put("processDefinitionId", processDefinition.getId());
            map.put("processDefinitionKey", processDefinition.getKey());
            map.put("processDefinitionName", processDefinition.getName());
            map.put("deployId", processDefinition.getDeploymentId());
            processDefinitionsList.add(map);
        });

        return Result.success(processDefinitionsList);
    }
}
